import {createRouter, createWebHashHistory} from 'vue-router'
import {useUserStore} from "@/stores/index.js";
import {Message} from "@arco-design/web-vue";
const routes = [
  {
    path: '/',
    component: () => import('../views/layout/IndexPage.vue'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import('../views/layout/HomePage.vue'),
      },
      {
        path: '/category',
        component: () => import('../views/layout/CategoryPage.vue'),
        meta: {
          requiresAuth: true,
        }
      },
      {
        path: '/create',
        component: () => import('../views/layout/CreatePage.vue'),
        meta: {
          requiresAuth: true,
        }
      },
      {
        path: '/user',
        component: () => import('../views/layout/UserPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/user/:id',
        component: () => import('../views/user/UserInfoPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/about',
        component: () => import('../views/layout/AboutPage.vue'),
      },
      {
        path: '/app/:id',
        component: () => import('../views/app/AppDetailPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/question/:id',
        component: () => import('../views/app/QuestionPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/edit/:id',
        component: () => import('../views/app/EditAppPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/score/:id',
        component: () => import('../views/app/ScoringPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/answer/:id',
        component: () => import('../views/app/AnswerPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/answer/result/:id',
        component: () => import('../views/app/ResultPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/answer/record/:id',
        component: () => import('../views/app/AnswerRecordPage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: '/search/:value',
        component: () => import('../views/layout/SearchPage.vue'),
        meta: {
          requiresAuth: true,
        }
      },
      {
        path: '/fans/:id',
        component: () => import('../views/user/FansPage.vue'),
        meta: {
          requiresAuth: true,
        }
      },
      {
        path: '/follow/:id',
        component: () => import('../views/user/FollowPage.vue'),
        meta: {
          requiresAuth: true,
        }
      },
      {
        path: '/question/ai/:id',
        component: () => import('../views/app/AiQuestionPage.vue'),
        meta: {
          requiresAuth: true,
        }
      },
      {
        path: '/score/ai/:id',
        component: () => import('../views/app/AiScoringPage.vue'),
        meta: {
          requiresAuth: true,
        }
      },
      {
        path: '/suggest',
        component: () => import('../views/other/SuggestPage.vue'),
        meta: {
          requiresAuth: true,
        }
      },
      {
        path: '/help',
        component: () => import('../views/other/HelpPage.vue'),
      }
    ],
  },
  {
    path: '/login',
    component: () => import('../views/login/LoginPage.vue'),
  },
  {
    path: '/register',
    component: () => import('../views/login/RegisterPage.vue'),
  },
  {
    path: '/register/email',
    component: () => import('../views/login/RegisterByEmailPage.vue'),
  },
  {
    path: '/agree',
    component: () => import('../views/other/UserAgreement.vue'),
  },
  {
    path: '/403',
    component: () => import('../views/other/ForbiddenPage.vue'),
  },
  {
    path: '/404',
    component: () => import('../views/other/NotFoundPage.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: routes
})

router.beforeEach((to, from, next) => {
  // 检查是否需要进行认证
  const userStore = useUserStore()

  // 如果路由不存在, 则跳转到 404 页面
  if (to.matched.length === 0) {
    return next('/404')
  }

  // 如果不需要认证, 则继续导航
  if (!to.matched.some(record => record.meta.requiresAuth)) {
    return next(); // 使用 return 确保只调用一次 next
  }

  // 检查是否存在 JWT 令牌
  if (!userStore.token) {
    Message.error('请先登录')
    // 如果没有 JWT 令牌, 则跳转到登录页面
    return next('/login') // 使用 return 确保只调用一次 next
  }

  // 如果有 JWT 令牌, 则继续导航
  next() // 只有在上述条件都不满足时才会到达这里
})


export default router
